package com.own.component.store.core.util;

import com.own.component.store.core.Store;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * PermissionCacheUtil
 *
 * @author chenxueli
 * @date 2024-04-09 10:19:00
 */
@Component
public class PermissionCacheUtil {

    private static final String TIMESTAMP_FIELD = "gt:";
    private static final String USER_ROLE_TIMESTAMP_FIELD = "gut:";

    @Resource
    private Store<String> store;

    /**
     * 设置信息的全局时间戳
     */
    public void setGlobalTimestamp() {
        store.value().set(TIMESTAMP_FIELD, String.valueOf(System.currentTimeMillis()));
    }

    /**
     * 获取信息的全局时间戳
     *
     * @return 时间戳
     */
    public String getGlobalTimestamp() {
        return store.value().get(TIMESTAMP_FIELD);
    }

    /**
     * 设置用户角色的时间戳
     *
     * @param userId 用户id
     */
    public void setUserRoleTimestamp(Long userId) {
        var key = USER_ROLE_TIMESTAMP_FIELD + userId;
        store.value().set(key, String.valueOf(System.currentTimeMillis()));
    }

    /**
     * 获取用户信息的时间戳
     *
     * @param userId 用户id
     * @return 时间戳
     */
    public String getUserRoleTimestamp(Long userId) {
        return store.value().get(USER_ROLE_TIMESTAMP_FIELD + userId);
    }

}
